package lab02.Test24;
import org.omg.CORBA.DATA_CONVERSION;

import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;


/**
 * Created by 李晓容 on 2017/10/31.
 */
public class Test {
        public static void main(String[] args) {

            //24、 	编程实现：计算两个日期的天数之差。编写Java应用程序，
            // 来计算任意两个日期的天数之差。要求：两个日期用字符串形式输入
            // （比如输入“20100101”表示2010年1月1日），需要将日期字符串的年、月、日分别转换为数字形式。
            Scanner scanner = new Scanner(System.in);
            StringBuffer year1 = new StringBuffer();
            StringBuffer year2 = new StringBuffer();
            StringBuffer month1 = new StringBuffer();
            StringBuffer month2 = new StringBuffer();
            StringBuffer day1 = new StringBuffer();
            StringBuffer day2 = new StringBuffer();

            System.out.println("请输入两个日期");
            String scanner1 = scanner.next();
            String scanner2 = scanner.next();
            for (int i = 0; i<4;i++) {
                year1.append(scanner1.charAt(i)) ;
                year2.append(scanner2.charAt(i));
            }


            for (int i = 4;i <6;i++){
                month1.append(scanner1.charAt(i));
                month2.append(scanner2.charAt(i));
            }

            for (int i = 6;i<8;i++){
                day1.append(scanner1.charAt(i));
                day2.append(scanner2.charAt(i));
            }
            int year1number = Integer.parseInt(String.valueOf(year1));
            int year2number = Integer.parseInt(String.valueOf(year2));
            int month1number =Integer.parseInt(String.valueOf(month1));
            int month2number =Integer.parseInt(String.valueOf(month2));
            int day1number =Integer.parseInt(String.valueOf(day1));
            int day2number =Integer.parseInt(String.valueOf(day2));


            Date date1 =new Date(year1number,month1number,day1number);
            Date date2 =new Date(year2number,month2number,day2number);
            Calendar datea = Calendar.getInstance();
            Calendar dateb = Calendar.getInstance();
            datea.setTime(date1);
            dateb.setTime(date2);
            datea.add(Calendar.YEAR,-1900);
            datea.add(Calendar.MONTH,-1);
            dateb.add(Calendar.YEAR,-1900);
            dateb.add(Calendar.MONTH,-1);

            System.out.println(datea.getTime());
            System.out.println(dateb.getTime());
            //分别得到两个时间的毫秒数 
            long s1 = datea.getTimeInMillis();
            long s2 = dateb.getTimeInMillis();
            long time = s1 - s2;
        /* long sl=datea.getTimeInMillis(); 
        long el=date2.getTimeInMillis(); 
        long ei=el-sl; 
        //根据毫秒数计算间隔天数 
        return (int)(ei/(1000*60*60*24)); */


            System.out.println("相差的天数为："+Math.abs(time)/(1000*60*60*24));
        }
    }

